package cn.fyl.system.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.system.domain.SystemDetail;
import cn.fyl.system.domain.Menu;
import cn.fyl.system.listener.PreAuthorize;
import cn.fyl.system.query.MenuQuery;
import cn.fyl.system.service.IMenuService;
import cn.fyl.system.service.ISystemDetailService;
import cn.fyl.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *菜单管理接口
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/menuTree")
    public List<Menu> menuTree(){
        return menuService.menuTree();

    }



    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @PreAuthorize(name = "菜单列表",sn = "menu:getAll") //value后面用来做权限校验
    public List<Menu> findAll(){
        return menuService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Menu findById(@PathVariable("id") Long id){
        return menuService.loadById(id);
    }


    /**
     * 添加和修改操作
     * @param Menu
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Menu Menu){
        try {
            if(Menu.getId()==null){
                menuService.save(Menu);
            }else {
                menuService.update(Menu);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<Menu> queryPage(@RequestBody MenuQuery query){
        try {
            return menuService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Menu>();
        }
    }



}
